const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { JqOrderdetail } from 'jqOrderdetail.model.js';

var time = require('../../utils/utils.js');

var jqOrderdetail = new JqOrderdetail();
var oid = '';
var stime = '';
var state = '';
var sDataTime = '';
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    clock: '',
    //倒计时渲染
    boolean: true,
    //订单未支付，未超时
    oid: '',
    stime: ''
  },
  onLoad: function (options) {
    oid = options.oid;
    var aid = options.aid;
    this.data.oid = oid;
    this.data.aid = aid;

    this._loadDate();
  },
  _loadDate: function () {
    jqOrderdetail.getjqQuery(this.data.oid, this.data.aid, data => {
      console.log(data)
      stime = data.stime;
      sDataTime = data.sDataTime; //过期时间
      //下单时间
      var stimeDate = time.formatTimeTwo(stime, 'Y/m/d h:m:s'); 
      //出游日期
      var uptimeDate = time.formatTimeTwo(data.uptime, 'Y/m/d');
      console.log(uptimeDate);
      var that = this;
      state = data.state;
      this.data.stime = stime;
      this.data.state = state;
      this.setData({
        'listfint': data,
        'stimeDate': stimeDate,
        'uptimeDate': uptimeDate
      });
      var NowTime = new Date().getTime(); //获取系统当前时间戳
      var _time = sDataTime * 1000 - NowTime; //下单以后剩余的时间
      console.log(NowTime,_time)
      count_down(that);
      /* 分秒倒计时 */
      function count_down(that) {
        if (state == '未付款') {
          //在未付款的情况下
          if (_time > 0) {
            //订单未失效
            that.setData({
              clock: date_format(_time),
              // 渲染倒计时时钟
              timeFlag: false,
              state1Flag: false,
              state2Flag: true
            });
          } else if (_time <= 0) {
            //订单已失效
            // jqOrderdetail.getJqStatus(oid, data => {
            //   tt.redirectTo({
            //     url: '../jqOrder/jqOrder'
            //   });
            // });
            that.setData({
              timeFlag: true,
              state1Flag: true,
              state2Flag: false
            });
          }
        }

       const time=setTimeout(function () {
          // 倒计时效果
          if(_time <= 0 ){
            clearTimeout(time)
          }
          _time -= 1000;
          count_down(that);
        }, 1000);
      }

      function date_format(micro_second) {
        var second = Math.floor(micro_second / 1000); // 秒数
        var hr = Math.floor(second / 3600); // 小时位
        var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60)); // 分钟位
        var sec = fill_zero_prefix(second - hr * 3600 - min * 60); // 秒位
        return "支付剩余时间：" + min + ":" + sec;
      } // 位数不足补零

      function fill_zero_prefix(num) {
        return num < 10 ? "0" + num : num;
      }
    });
  },

  
  QuXiaoClick: function () {
    tt.showModal({
      title: '提示',
      content: '确定取消此订单？',
      success: function (res) {
        if (res.confirm) {
          jqOrderdetail.getJqStatus(oid, data => {});
          tt.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    });
  },

  /*
   *开始支付
   * params:
   * id - {int}订单id
   */
  zhifuClick: function (e) {
    var that = this;
    var id = e.target.dataset.oid;
    jqOrderdetail.execPay(id, statusCode => {
      console.log(statusCode);

      if (statusCode != 0) {
        da;
        tt.showToast({
          title: '支付成功'
        }); //显示2秒隐藏加载提示

        setTimeout(function () {
          tt.hideToast();
        }, 2000);
        var flag = statusCode == 1;
        tt.navigateTo({
          url: '../jqOrder/jqOrder'
        });
      } else {
        that._orderFail(statusCode); // 下单失败

      }
    });
  },
  _orderFail: function (statusCode) {
    tt.showModal({
      title: '下单失败',
      content: '请联系客服 400-800-7000',
      showCancel: false,
      success: function (res) {
        tt.navigateTo({
          url: '../jqOrder/jqOrder'
        });
      }
    });
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  }
});